package com.sdksoft.readcard

import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import android.view.Menu
import android.view.MenuItem
import com.csht.bean.ResultData
import com.csht.bean.ResultSocketData
import com.csht.enums.ReadCardType
import com.csht.enums.StatusCode
import com.csht.fragment.NFCAnimFragment
import com.csht.jni.NativeUtils
import com.csht.listener.InitListener
import com.csht.utils.BytesUtils
import com.csht.utils.GzipUtils
import com.csht.utils.HHTipUtils
import com.csht.utils.ImageUtil
import com.csht.utils.JsonUtils
import com.csht.utils.LogUtils
import java.io.ByteArrayOutputStream

class NFCActivity : BaseIDCardActivity(), InitListener {

    private var initSuccess = false
    private val nfcAnimFragment = NFCAnimFragment()

    override fun initView() {
        super.initView()
        setTitleText("NFC读卡")
        readSettingBuilder.buildReadCardType(ReadCardType.NFC)
        readCardManager.readCardInit( this)
        // 读取assets目录文件内容
        Thread {
            try {
                val inputStream = resources.assets.open("data1.txt")
                val buffer = ByteArray(4096)
                val outputStream = ByteArrayOutputStream()
                var i = -1
                while (inputStream.read(buffer).also {
                        LogUtils.e("SM4: ", "SM4_ECB_PKCS5 读取数据长度 ：$it")
                        i = it
                    } != -1) {
                    outputStream.write(buffer, 0, i)
                }
                LogUtils.e("SM4: ", "SM4_ECB_PKCS5 读取数据结束，总长度：${outputStream.size()}")
                inputStream.close()

                val gzip = GzipUtils.uncompress(String(outputStream.toByteArray()))
                LogUtils.e("SM4: ", "SM4_ECB_PKCS5 解密 ：$gzip")

                val resultSocketData: ResultSocketData? = JsonUtils.gson.fromJson(gzip, ResultSocketData::class.java)
                resultSocketData?.idCard?.let {
                    LogUtils.e("SM4: ", "idCard ：${JsonUtils.gson.toJson(it)}")
                }
            } catch (e: Exception) {
                LogUtils.e("SM4: ", "SM4_ECB_PKCS5 解密异常 ：${e.message}")
            }
        }.start()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        super.onCreateOptionsMenu(menu)
        menuInflater.inflate(R.menu.nfc_setting, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.nfc_area -> {
                // 根据手机品牌跳转NFC区域示意图
                val url = nfcAnimFragment.getNFCViewRect()
                if (TextUtils.isEmpty(url)) {
                    HHTipUtils.getInstance().showToast(this, "该型号暂无NFC区域数据")
                } else {
                    // 跳转外部浏览器
                    val intent = Intent(Intent.ACTION_VIEW)
                    intent.data = Uri.parse(url)
                    startActivity(intent)
                }
            }
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        intent?.let {
            if (!initSuccess) {
                HHTipUtils.getInstance().showToast(this, "请先初始化")
                return
            }
            clear()
            readCardManager.readCardStart(intent, this)
        }
    }

    override fun onResponse(code: Int, msg: String) {
        mHandler.post {
            when (code) {
                StatusCode.ININ_ING.code -> {
                    appendLog("初始化中,$msg")
                }

                StatusCode.ININ_OK.code -> {
                    initSuccess = true
                    appendLog("初始化成功")
                }

                else -> {
                    initSuccess = false
                    appendLog("初始化失败$code,$msg")
                }
            }
        }
    }
}